package com.infoland.afanmall.model.my.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.entity.RefreshResponse;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.UserKit;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.lzy.okgo.OkGo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class FindPasswordActivity extends AppCompatActivity {
    @BindView(R.id.find_back)
    ImageView findBack;
    @BindView(R.id.find_ed1)
    EditText findEd1;
    @BindView(R.id.find_txt1)
    TextView findTxt1;
    @BindView(R.id.find_ll2)
    LinearLayout findLl2;
    @BindView(R.id.find_ed2)
    EditText findEd2;
    @BindView(R.id.find_bt)
    LinearLayout findBt;
    String phone;
    String code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.password_find_activity);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        ToolsKit.setTypeNumber(findEd1);
        ToolsKit.setTypeNumber(findEd2);
    }

    @OnClick({R.id.find_back, R.id.find_ed1, R.id.find_txt1, R.id.find_ll2, R.id.find_ed2, R.id.find_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            /***退出*/
            case R.id.find_back:
                finish();
                break;
            /***发送验证码*/
            case R.id.find_ll2:
                sendValidateCode();
                break;
            /***发送验证码*/
            case R.id.find_txt1:
                sendValidateCode();
                break;
            /***下一步*/
            case R.id.find_bt:
                int visibility = findLl2.getVisibility();
                LogKit.E("visibility   ", "  " + visibility);
                LogKit.E("View.VISIBLE   ", "  " + View.VISIBLE);
                if (visibility == View.VISIBLE) {
                    LogKit.E("checkYanZheng   ", " checkYanZheng ");
                    if (ToolsKit.isPhone(findEd1.getText().toString())) {
                        if (ToolsKit.isYzM(findEd2.getText().toString())) {
                            checkYanZheng();
                        } else {
                            ToastUtils.showShort("验证码错误");
                        }
                    } else {
                        ToastUtils.showShort("手机号错误");
                    }
                } else {
                    if (ToolsKit.isPassword(findEd1.getText().toString()) && ToolsKit.isPassword(findEd2.getText().toString())) {
                        if (findEd1.getText().toString().equals(findEd2.getText().toString())) {
                            LogKit.E("sendUpdatePassword   ", " sendUpdatePassword ");
                            sendUpdatePassword();
                        } else {
                            ToastUtils.showShort("两次密码不一致");
                        }


                    } else {
                        ToastUtils.showShort("密码长度在 6-16");
                    }
                }
                break;
        }
    }

    /***发送请求修改密码**/
    private void sendUpdatePassword() {
        JSONObject para = new JSONObject();
        para.put("phone", phone);
        para.put("code", code);
        para.put("password", ToolsKit.getSHA1String(findEd1.getText().toString()));
        new OkHttpKit().send(
                response200 -> {
                    LogKit.E("  response ", "  " + response200);
                    RefreshResponse refreshResponse = JSONObject.parseObject(response200, RefreshResponse.class);
                    if (refreshResponse.isSuccess()) {
                        UserKit.setUserCacheYes(refreshResponse.getObject().getAccess_token(), refreshResponse.getObject().getRefresh_token());
                        ToastUtils.showShort("修改完成");
                        finish();
                    } else {
                        ToastUtils.showShort(refreshResponse.getMessage());
                    }
                },
                success401 -> {
                    ToastUtils.showShort("验证失败");
                    LogKit.E("success401 ---->   ", "  " + success401);
                },
                error -> {
                    ToastUtils.showShort("验证失败");
                    LogKit.E("error ---->   ", "  " + error);
                }
        ).post(CONST.REST_PASSWORD, para, this);
    }

    /***验证手机号和验证是否正确**/
    private void checkYanZheng() {
        JSONObject para = new JSONObject();
        phone = findEd1.getText().toString();
        code = findEd2.getText().toString();
        para.put("phone", phone);
        para.put("code", code);
        para.put("type", 3);

        JSONObject params = new JSONObject();
        params.put("pageNumber", 1);
        params.put("pageSize", 10);
        new OkHttpKit().send(
                response200 -> {
                    JSONObject jsonObject1 = JSONObject.parseObject(response200);
                    LogKit.E("jsonObject1 ", "  " + jsonObject1.toJSONString());
                    if (jsonObject1.getBoolean("success")) {
//                    findEd1.clearFocus();
                        findEd2.clearFocus();
                        ToolsKit.setTypePassWord(findEd1);
                        ToolsKit.setTypePassWord(findEd2);
                        findLl2.setVisibility(View.GONE);
                        findEd1.setText("");
                        findEd2.setText("");
                        findEd1.setHint("密码");
                        findEd2.setHint("确认密码");
                        ToastUtils.showShort("请输入密码");
                    } else {
                        ToastUtils.showShort(jsonObject1.getString("message"));
                    }

                },
                success401 -> {
                    LogKit.E("   e", "  " + success401);
                    ToastUtils.showShort("验证失败");
                },
                error -> {
                    LogKit.E("   e", "  " + error);
                    ToastUtils.showShort("验证失败");
                }
        ).post(CONST.CHECK_VALIDATE_CODE + UserKit.getAccessToken(), para, this);
    }

    /***发送验证码**/
    private void sendValidateCode() {
        JSONObject para = new JSONObject();
        LogKit.E("  sendValidateCode ", "sendValidateCode  ");
        phone = findEd1.getText().toString();
        if (!ToolsKit.isPhone(phone)) {
            ToastUtils.showShort("手机号输入错误");
        } else {
            para.put("phone", phone);
            para.put("type", "3");
            LogKit.E("  para ", "  " + para.toJSONString());
            UserKit.sendValidateCode(para, findLl2, findTxt1, 120 * 1000, 1000, "再次发送", this);
        }
    }

    @Override
    public void onDestroy() {
        OkGo.getInstance().cancelTag(this);
        super.onDestroy();
    }
}
